package com.imy.luascript.app.ui.test;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.imy.lua.helper.opencv.ImageFindHelper;
import com.imy.lua.lib.image.UDImage;
import com.imy.luascript.R;

import java.io.File;

public class OpenCVTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideSystemNav();
        File externalStorageDirectory = Environment.getExternalStorageDirectory();
        String absolutePath = externalStorageDirectory.getAbsolutePath();
        String source = absolutePath + "/Pictures/111.png";
        Bitmap bitmap = BitmapFactory.decodeFile(source);
        setContentView(R.layout.activity_open_cv_test);
        ImageView imageView = findViewById(R.id.image_view);
        View rectView = findViewById(R.id.rect_view);
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String filePath = absolutePath + "/Pictures/222.png";
                Bitmap bitmap1 = BitmapFactory.decodeFile(filePath);
                Point image = ImageFindHelper.get().findImage(source, filePath);
                if (image==null){
                    return;
                }
                int width = bitmap1.getWidth();
                int height = bitmap1.getHeight();
                ViewGroup.LayoutParams layoutParams = rectView.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                rectView.setLayoutParams(layoutParams);
                rectView.setVisibility(View.VISIBLE);
                rectView.setX(image.x);
                rectView.setY(image.y);
            }
        });


        imageView.setImageBitmap(bitmap);
    }

    private void hideSystemNav() {
        if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = getWindow().getDecorView();
            final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(flags);
            decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if (isFinishing()) {
                        return;
                    }
                    if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                        decorView.setSystemUiVisibility(getShowInputFlags());
                    } else if (visibility == 4) {
                        decorView.setSystemUiVisibility(flags);
                    }
                }
            });
        }

    }

    private int getShowInputFlags() {
        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        return flags;
    }
}
